﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Common
{
    public class OnlineStatusHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        /// <summary>
        /// 当前用户名
        /// </summary>
        public string cmu_LoginName
        {
            get
            {
                return HttpContext.Current.User.Identity.Name;
            }
        }

        /// <summary>
        /// 请求的操作
        /// </summary>
        public string action
        {
            get
            {
                return HttpContext.Current.Request.QueryString["action"];
            }
        }

        /// <summary>
        /// 在线状态 1:在线;0:隐身
        /// </summary>
        public string OnlineStatus
        {
            get
            {
                return HttpContext.Current.Request.QueryString["onlinestatus"];
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            using (SqlConnection BAAConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["oldbaa"].ToString()))
            {
                if (!string.IsNullOrEmpty(cmu_LoginName))
                {
                    switch (action)
                    {
                        case "change":
                            ChangeOnlineStatus(OnlineStatus);
                            context.Response.Write("1");
                            break;
                        default:
                            BAAConnection.Open();
                            SqlCommand cmd = new SqlCommand();
                            cmd.Connection = BAAConnection;
                            cmd.CommandText = "select invisible,globallastactivity from dnt_users where username = @cmu_LoginName";
                            cmd.CommandType = CommandType.Text;
                            cmd.Parameters.AddWithValue("@cmu_LoginName", cmu_LoginName);
                            int invisible = 0;
                            DateTime lastactivity = new DateTime();
                            int onlineStatus = 0;
                            using (SqlDataReader read = cmd.ExecuteReader())
                            {
                                if (read.Read())
                                {
                                    invisible = int.Parse(read["invisible"].ToString());
                                    lastactivity = DateTime.Parse(read["globallastactivity"].ToString());
                                    // 最后活动时间20分钟内的为在线, 否则不在线
                                    if (DateTime.Now.AddMinutes(-20) <= lastactivity && invisible != 1)
                                    {
                                        onlineStatus = 1;
                                    }
                                    else
                                    {
                                        onlineStatus = 0;
                                    }
                                }
                            }
                            context.Response.Write(onlineStatus.ToString());
                            break;
                    }
                }
                else
                {
                    context.Response.End();
                    return;
                }
            }
        }

        /// <summary>
        /// 更改在线状态
        /// </summary>
        /// <param name="onlineStatus">1:在线;0:隐身</param>
        public void ChangeOnlineStatus(string onlineStatus)
        {
            //using (SqlConnection BAAConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["oldbaa"].ToString()))
            //{
            //    BAAConnection.Open();
            //    SqlCommand cmd = new SqlCommand();
            //    cmd.Connection = BAAConnection;
            //    cmd.CommandText = "update dnt_users set invisible = @invisible,globallastactivity = getdate() where username = @cmu_LoginName";
            //    cmd.CommandType = CommandType.Text;
            //    cmd.Parameters.AddWithValue("@cmu_LoginName", cmu_LoginName);
            //    cmd.Parameters.AddWithValue("@invisible", onlineStatus=="1"?"0":"1");
            //    cmd.ExecuteNonQuery();
            //}
            ChangeOnlineStatus(onlineStatus, cmu_LoginName);
        }

        /// <summary>
        /// 更改在线状态
        /// </summary>
        /// <param name="onlineStatus">1:在线;0:隐身</param>
        /// <param name="cmu_LoginName">用户名</param>
        public void ChangeOnlineStatus(string onlineStatus, string username)
        {
            using (SqlConnection BAAConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["oldbaa"].ToString()))
            {
                BAAConnection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = BAAConnection;
                cmd.CommandText = "update dnt_users set invisible = @invisible,globallastactivity = getdate() where username = @cmu_LoginName";
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@cmu_LoginName", username);
                cmd.Parameters.AddWithValue("@invisible", onlineStatus == "1" ? "0" : "1");
                cmd.ExecuteNonQuery();
            }
        }

    }
}
